package cn.glc.daily.life.web.controller.publicapi;

import cn.glc.daily.life.common.config.properties.WeiboAuthProperties;
import cn.glc.daily.life.common.config.properties.WeiboUrlProperties;
import cn.glc.daily.life.common.constans.Developers;
import cn.glc.daily.life.common.utils.JsonResult;
import cn.glc.daily.life.model.dto.LoginDto;
import cn.glc.daily.life.model.enums.LoginTypeEnums;
import cn.glc.daily.life.web.service.user.UserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * token 回调地址
 *
 * @author simon
 * @date 2020/11/16 16:09
 * @copyright:
 * @modifier
 * @motto a no talent, people who have to study hard
 */
@Slf4j
@RestController
@RequestMapping(value = "/thirdToken")
public class TokenCallbackController {

    @Autowired
    private WeiboAuthProperties weiboAuthProperties;
    @Autowired
    private WeiboUrlProperties weiboUrlProperties;
    @Autowired
    private UserService userService;

    /**
     * // TODO: 2020/12/2 还需要调整下,前端怎么调的,能获取返回值么
     * 微博用户登录回调地址,重定向到登录地址
     * 步骤
     * 1.页面登录授权
     * 2.微博回调该接口并传送code
     * 3.通过code获取访问token(access_token)
     * 4.通过access_token,调用用户信息接口
     * 5.登录成功
     *
     * @param code
     * @param state
     * @return
     */
    @GetMapping(value = "/callback/weibo")
    @ApiOperation(value = "微博用户登录回调", notes = Developers.CY)
    public JsonResult login(String code, String state) {
        log.info("code:{},state:{}", code, state);
        LoginDto loginDto = new LoginDto();
        loginDto.setIdentify("uid");
        loginDto.setCredential(code);
        loginDto.setType(LoginTypeEnums.WEI_BO_WEB);
        userService.unifiedLogin(loginDto);
        return JsonResult.returnSuccess();
    }
}

